﻿// Decompiled with JetBrains decompiler
// Type: NetickEditor.NetickPrefabTools
// Assembly: Netick.Unity.Editor, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: 0658BB4A-05C4-428D-9BD6-82E94959F5A1
// Assembly location: D:\WorkSpaceDemo\UPM\NetickForUnity\Netick\Editor\Netick.Unity.Editor.dll

using Netick.Unity;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;

#nullable disable
namespace NetickEditor;

public static class NetickPrefabTools
{
  public static void LoadAllNetworkedPrefabs(NetickConfig config)
  {
    bool flag = false;
    List<NetworkObject> networkObjectList = config.Prefabs != null ? config.Prefabs.ToList<NetworkObject>() : new List<NetworkObject>();
    string[] assets = AssetDatabase.FindAssets("t:Prefab", new string[1]
    {
      config.PrefabsPath
    });
    config.Prefabs = new List<NetworkObject>();
    foreach (string str in assets)
    {
      GameObject gameObject = AssetDatabase.LoadMainAssetAtPath(AssetDatabase.GUIDToAssetPath(str)) as GameObject;
      NetworkObject component;
      if ((Object) gameObject.transform.parent == (Object) null && gameObject.TryGetComponent<NetworkObject>(out component))
      {
        config.Prefabs.Add(component);
        NetickPrefabTools.ProcessPrefab(component);
      }
    }
    List<NetworkObject> list = config.Prefabs.ToList<NetworkObject>();
    if (list.Count != networkObjectList.Count)
      flag = true;
    if (list.Count == networkObjectList.Count)
    {
      for (int index = 0; index < list.Count; ++index)
      {
        if ((Object) list[index] != (Object) networkObjectList[index])
          flag = true;
      }
    }
    if (!flag)
      return;
    EditorUtility.SetDirty((Object) config);
    AssetDatabase.SaveAssets();
    AssetDatabase.Refresh();
  }

  internal static void ProcessPrefab(NetworkObject obj)
  {
    SerializedObject serializedObject = new SerializedObject((Object) obj);
    bool wasDirty = false;
    foreach (NetworkAnimator componentsInChild in obj.GetComponentsInChildren<NetworkAnimator>())
    {
      if (NetickEditorAnimationUtils.SetupAnimator(componentsInChild))
        wasDirty = true;
    }
    foreach (Component component in obj.transform)
    {
      if (NetickPrefabTools.ProcessPrefabChild(component.gameObject, obj, ref wasDirty))
        wasDirty = true;
    }
    if (!wasDirty)
      return;
    serializedObject.ApplyModifiedProperties();
    EditorUtility.SetDirty((Object) obj);
  }

  internal static bool ProcessPrefabChild(GameObject obj, NetworkObject root, ref bool wasDirty)
  {
    bool flag = false;
    NetworkObject component1 = obj.GetComponent<NetworkObject>();
    if ((Object) component1 != (Object) null)
    {
      if (component1.SceneId != -1)
      {
        component1.SceneId = -1;
        flag = true;
      }
      SerializedObject serializedObject = new SerializedObject((Object) component1);
      foreach (NetworkAnimator componentsInChild in obj.GetComponentsInChildren<NetworkAnimator>())
      {
        if (NetickEditorAnimationUtils.SetupAnimator(componentsInChild))
          flag = true;
      }
    }
    foreach (Component component2 in obj.transform)
      NetickPrefabTools.ProcessPrefabChild(component2.gameObject, root, ref wasDirty);
    return flag;
  }
}
